using Level;
using TMPro;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.UI;

namespace UI
{
    public class GameOverUi : MonoBehaviour
    {
        [SerializeField] private GameOverState gameOverState;
        
        [SerializeField] private TextMeshProUGUI liveTimeText;
        [SerializeField] private Button exitButton;
        [SerializeField] private Button restartButton;
        
        [SerializeField] private SceneLoader sceneLoader;

        private UiFader _uiFader;


        private void Start()
        {
            _uiFader = GetComponent<UiFader>();

            gameOverState.GameOver += Global_GameOver;
            
            restartButton.onClick.AddListener(Restart);
            exitButton.onClick.AddListener(Exit);
        }

        private void OnDestroy()
        {
            gameOverState.GameOver -= Global_GameOver;
        }

        private void Global_GameOver()
        {
            if (LocalizationSettings.SelectedLocale == LocalizationSettings.AvailableLocales.Locales[0])
            {
                liveTimeText.text = $"生存时间: {gameOverState.LiveTime:0} 秒";
            }
            else
            {
                liveTimeText.text = $"Survived for {gameOverState.LiveTime:0}s";
            }
            _uiFader.Show();
        }
        
        private void Restart()
        {
            sceneLoader.LoadScene(gameObject.scene.buildIndex);
        }

        private void Exit()
        {
            sceneLoader.LoadScene(0);
        }
    }
}